﻿using NodaTime;
using System;

namespace Kz.Telegram.Bot.Infrastructure.Helpers
{
    public static class DateTimeHelper
    {

        /// <summary>
        /// 获取指定时间的时间戳（精度到毫秒）表示形式
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <param name="bflag">true 到秒  false 毫秒</param>
        /// <param name="isUtcTime">是否</param>
        /// <returns></returns>
        public static long GetStampFromDateTime(this DateTime time, bool bflag = false, bool isUtcTime = false)
        {
            if (isUtcTime)
                return time.GetUtcTimeStamp(bflag);
            else
                return time.GetTimeStamp(bflag);
        }

        /// <summary>
        /// 非UTC时间  获取指定时间的时间戳（精度到毫秒）表示形式
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <param name="bflag">true 到秒  false 毫秒</param>
        /// <returns></returns>
        private static long GetUtcTimeStamp(this DateTime time, bool bflag = false)
        {
            TimeSpan ts = time - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            if (bflag)
                ret = Convert.ToInt64(ts.TotalSeconds).ToString();
            else
                ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return Convert.ToInt64(ret);
        }

        /// <summary>
        /// UTC时间  获取指定时间的时间戳（精度到毫秒）表示形式
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <param name="bflag">true 到秒  false 毫秒</param>
        /// <returns></returns>
        private static long GetTimeStamp(this DateTime time, bool bflag = false)
        {
            TimeSpan ts = time.AddHours(-8) - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            if (bflag)
                ret = Convert.ToInt64(ts.TotalSeconds).ToString();
            else
                ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return Convert.ToInt64(ret);
        }

        /// <summary>
        /// 将时间戳(精度秒)转为日期时间形式
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeFromStampA(long timeStamp)
        {
            //System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), TimeZoneInfo.Local); // 当地时区
            //return startTime.AddSeconds(timeStamp);

            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddSeconds(timeStamp);
        }

        /// <summary>
        /// 将时间戳(精度毫秒)转为日期时间形式
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeFromStamp(long timeStamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddMilliseconds(timeStamp);
        }


        /// <summary>
        /// 当前time字符串  例：08:59:01  将会转换未 85901
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int TimeStrToNumber(this string time)
        {
            return Convert.ToInt32(time.Replace(":", ""));
        }

        /// <summary>
        /// 将DATETIME转为yyyyMMdd数字类型  
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static int DateTimeToDateNumber(this DateTime dateTime)
        {
            return Convert.ToInt32(dateTime.ToString("yyyyMMdd"));
        }

        public static double DiffTotalSeconds(this DateTime DateTime1, DateTime DateTime2)
        {
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            return ts.TotalSeconds;
        }

        public static DateTime ToCstTime(this DateTime time)
        {
            Instant now = SystemClock.Instance.GetCurrentInstant();
            var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
            return now.InZone(shanghaiZone).ToDateTimeUnspecified();
        }
    }
}
